﻿using System;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(Attack))]
public class AIAttack : AIBehaviour
{
    private float nextAttackTime;

    protected override void Awake()
    {
        base.Awake();
    }

    public bool CanWeaponHitTarget()
    {
        Vector3 rhs = this.target.transform.position - base.position;
        return (((base.weapon != null) && (rhs.magnitude < (base.attack.weapon.attackRange + 0.5f))) && (Vector3.Dot(base.transform.forward, rhs) > 0.75f));
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    private void UNetVersion()
    {
    }

    private void Update()
    {
        if (((base.inventory != null) && (base.inventory.weapon == null)) && (base.inventory.secondWeapon != null))
        {
            base.inventory.SwitchWeapon();
        }
        if (((base.weapon != null) && ((this.target != null) && (this.target.gameObject.layer != GameLayers.Item.index))) && (this.CanWeaponHitTarget() && (Time.time > this.nextAttackTime)))
        {
            base.attack.DoAttack();
            this.nextAttackTime = Time.time + (base.weapon.cooldown * UnityEngine.Random.Range(1, 5));
        }
    }

    private GameObject target
    {
        get
        {
            return ((base.targetDetector == null) ? null : base.targetDetector.target);
        }
    }
}

